package com.example.tabbar.adapter

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.CheckBox
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.tabbar.R
import com.example.tabbar.databinding.ItemAuditBinding
import com.example.tabbar.room.entity.SamplingData

class AuditAdapter: RecyclerView.Adapter<AuditAdapter.ViewHolder>() {
    private var items: MutableList<SamplingData> = ArrayList()
    private var selectedPositions: MutableSet<Int> = HashSet() // 用于跟踪选中项的位置
    private var selectAllListener: ((Boolean) -> Unit)? = null // 添加全选状态监听器
    fun setOnItemClickListener(listener: (Boolean) -> Unit) {
        this.selectAllListener = listener
    }
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val binding = ItemAuditBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return ViewHolder(binding)
    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val item = items[position]

            holder.serialNum.text = (position + 1).toString()
            if(item.result == true) {
                holder.result.setImageResource(R.drawable.green_dot)
            } else {
                holder.result.setImageResource(R.drawable.red_dot)
            }


        // 设置选中状态
        holder.checkBox.isChecked = selectedPositions.contains(position)

        // 设置选择状态改变监听器
        holder.checkBox.setOnCheckedChangeListener { _, isChecked ->
            if (isChecked) {
                selectedPositions.add(position)
            } else {
                selectedPositions.remove(position)
            }

            // 检查是否需要更新全选按钮状态
            notifySelectAllStateChanged()
        }
    }

    // 全选/取消全选方法
    fun selectAll(select: Boolean) {
        selectedPositions.clear()
        if (select) {
            // 选中所有项
            for (i in 0 until items.size) {
                selectedPositions.add(i)
            }
        }
        notifyDataSetChanged()
        selectAllListener?.invoke(select)
    }

    // 检查是否全选
    private fun isAllSelected(): Boolean {
        return selectedPositions.size == items.size && items.isNotEmpty()
    }
    // 通知全选状态改变
    private fun notifySelectAllStateChanged() {
        val isAllSelected = isAllSelected()
        selectAllListener?.invoke(isAllSelected)
    }

    override fun getItemCount(): Int {
        return items.size ?: 0
    }

    fun setItems(items: List<SamplingData>?) {
        this.items.clear()
        if (items != null) {
            this.items.addAll( items)
            notifyDataSetChanged()
        }
    }


    class ViewHolder(binding: ItemAuditBinding)
        : RecyclerView.ViewHolder(binding.root) {
        var checkBox: CheckBox = binding.isSelect
        var serialNum = binding.serialNumber
        var date:TextView = binding.date
        var group:TextView = binding.group
        var role :TextView = binding.role
        var result :ImageView= binding.results
    }

}